﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Sgr;
using Sgr.EntityFrameworkCore;
using Sgr.Trackers;
using Sgr.Trackers.BackGroundTasks;
using Sgr.Trackers.Domain;
using Sgr.Trackers.Infrastructure;
using Sgr.Trackers.Infrastructure.Repositories;

namespace Microsoft.Extensions.DependencyInjection
{
    /// <summary>
    ///
    /// </summary>
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection UseMessageTrackerLogs<TContext>(this IServiceCollection services) where TContext : UnitOfWorkDbContext
        {
            services.AddOptions<TrackerOptions>()
                .BindConfiguration("Sgr:MessageTrackerLogs");

            services.TryAddScoped<IMessageTrackerLogRepository, MessageTrackerLogRepository<TContext>>();
            services.TryAddEnumerable(ServiceDescriptor.Singleton<IEntityFrameworkTypeProvider, MessageTrackerLogEntityFrameworkTypeProvider>());
            // 清理资源
            services.TryAddEnumerable(ServiceDescriptor.Scoped<ICleanupHandle, MessageTrackerLogCleanupHandle>());

            return services;
        }
    }
}